package tests;

import static org.junit.Assert.*;

import java.util.LinkedList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import reit.main.Parser;
import reit.main.RentalRequest;
import reit.main.RentalRequests;

public class RentalRequestsTest {

	private reit.xml.customer.CustomersGroups fCustomersGroups = null;
	private LinkedList<RentalRequest> fRentalRequestsData = null;
	private RentalRequests fRentalRequests=null;
	@Before
	public void setUp() throws Exception {

		String path = "src/xmls/CustomersGroups.xml";
		try{
			JAXBContext jc = JAXBContext.newInstance(reit.xml.customer.CustomersGroups.class);
			Unmarshaller um = jc.createUnmarshaller();
			JAXBElement<reit.xml.customer.CustomersGroups> root = um.unmarshal(new StreamSource(path), reit.xml.customer.CustomersGroups.class); // creating a JAXBelement to store Assets no @XMLRootElement forces to do this from xml and putting value in assets
			this.fCustomersGroups  =  root.getValue();
			if (fCustomersGroups.getCustomerGroups().getCustomerGroupDetails().size()>0){
				this.fRentalRequestsData = Parser.createRentalRequestsContainer(fCustomersGroups.getCustomerGroups().getCustomerGroupDetails().get(0));
				this.fRentalRequests= new RentalRequests(this.fRentalRequestsData);
			}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		} 
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testRentalRequests() {
		int i=0;
		i++;
		assertTrue(this.fRentalRequests!=null);
	}

	@Test
	public void testGetRentalRequest() {
		RentalRequest RR = null;
	
			RR = this.fRentalRequests.take();
	
		if (null != RR){
			assertEquals(3,RR.getRoomSize(),0);
		}
		else{
			fail("problem taking rental request from queue");
		}
		

		RentalRequest RR2=null;
		RR2 = this.fRentalRequests.take();
		assertTrue(RR != RR2 && RR2!=null);

		
	}

	@Test
	public void testReleseRequest() {
		assertTrue(this.fRentalRequests.size()>0);
		RentalRequest RR=null;
		while (this.fRentalRequests.size()>0){
			RR = this.fRentalRequests.take();
		}
		this.fRentalRequests.add(RR);
		assertTrue(this.fRentalRequests.size()>0);
		RR = this.fRentalRequests.take();
		assertTrue(this.fRentalRequests.size()==0);
	}

}
